/**
 * 2012-2-16
 * @author 李亚松
 * @email  lysong01@gmail.com
 * Creative.java
 * description	主要采用正则表达式从网页中匹配出csv所需相关信息
 */
package liyasong.tool;
/*
 * 
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MakeCsv {

	//百家姓
	private String firstName = "[李王张刘陈杨赵黄周吴徐孙胡朱高林何郭马罗梁宋郑谢韩唐冯于董萧程" +
			"柴袁邓许傅沈曾彭吕苏卢蒋蔡贾丁魏薛叶阎余潘杜戴夏钟汪田任姜范石姚谭廖邹熊金陆郝孔白崔康毛邱秦江史顾侯邵孟龙万段曹钱汤尹黎常乔贺赖龚陶文辛岳]";
	//匹配中文姓名正则表达式
	private String nameRegex = firstName + "[\\u4e00-\\u9fa5]{1,2}";
	
	private String phoneRegex = "\\d{11}";
	
	private String googleCsv = "Name,Given Name,Additional Name,Family Name,Yomi Name,Given Name Yomi," +
			"Additional Name Yomi,Family Name Yomi,Name Prefix,Name Suffix,Initials,Nickname," +
			"Short Name,Maiden Name,Birthday,Gender,Location,Billing Information," +
			"Directory Server,Mileage,Occupation,Hobby,Sensitivity,Priority,Subject,Notes," +
			"Group Membership,E-mail 1 - Type,E-mail 1 - Value,Phone 1 - Type,Phone 1 - Value";
	
	private String fetionCsv = "FetionId,MobileNo,Name,Nickname";
	
	/*
	 * 从参数文件里匹配中文名字，返回一个集合
	 */
	private Set<String> matchName(String filename) throws IOException {
		Pattern namePattern = Pattern.compile(nameRegex);
		BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
		Set<String> names = new LinkedHashSet<String>();
		String str = "";
		while((str = br.readLine()) != null) {
			Matcher matcher = namePattern.matcher(str);
			while (matcher.find()) {
					names.add(matcher.group());
			}
			
		}
		br.close();
		return names;
	}
	
	/*
	 * 从所给参数文件匹配手机号，返回一个集合
	 */
	private Set<String> matchTelephone(String filename) throws IOException {
		Pattern phonePattern = Pattern.compile(phoneRegex);
		BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
		Set<String> phones = new LinkedHashSet<String>();
		String str = "";
		while((str = br.readLine()) != null) {
			Matcher matcher = phonePattern.matcher(str);
//			System.out.println(matcher.group());
			while (matcher.find()) {
					phones.add(matcher.group());
			}
		}
		br.close();
		return phones;
	}
	
	public void makeGoogleCsv(String filename) throws IOException {
		BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filename + "Google.csv")));
		Set<String> names = matchName(filename + ".html");
		Set<String> phones = matchTelephone(filename + ".html");
		if (names.size() == phones.size()) {
			Object[] namesStr = names.toArray();
			Object[] phonesStr = phones.toArray();
			bw.write(googleCsv + "\n");
			for (int i = 0; i < namesStr.length; i++) {
				String name = namesStr[i].toString();
				bw.write(name+","+name.substring(1, name.length())+",,"+name.charAt(0)+
						",,,,,,,,,,,,,,,,,,,,,,,* My Contacts ::: 高中同学,,,Mobile,"+phonesStr[i]+"\n");
			}
			System.out.println("共有" + names.size() + "个联系人记录");
		} else {
			System.out.println("名字个数与电话个数不匹配");
		}
		bw.close();
	}

	public void makeFetionCsv(String filename) throws IOException {
		BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filename + "Fetion.csv")));
		Set<String> names = matchName(filename + ".html");
		Set<String> phones = matchTelephone(filename + ".html");
		if (names.size() == phones.size()) {
			Object[] namesStr = names.toArray();
			Object[] phonesStr = phones.toArray();
			bw.write(fetionCsv + "\n");
			for (int i = 0; i < namesStr.length; i++) {
				String name = namesStr[i].toString();
				bw.write(","+phonesStr[i]+","+name+","+name+"\n");
			}
			System.out.println("共有" + names.size() + "个联系人记录");
		} else {
			System.out.println("名字个数与电话个数不匹配");
		}
		bw.close();
	}
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		MakeCsv mc = new MakeCsv();
//		mc.makeGoogleCsv("17");
		mc.makeFetionCsv("17");
//		System.out.println(mc.matchName("17.html").size());
//		System.out.println(mc.matchTelephone("17.html").size());
	}

}

